import { ui } from "../../ui/layaMaxUI";
import Game from "../../core_wq/game/Game";
import EffectUtil from "../../core_wq/utils/EffectUtil";
import GameEvent from "../../core_wq/game/GameEvent";
import PlayerScript from "../../core_wq/component/PlayerScript";
import MonsterData from "../../core_wq/game/data/MonsterData";
import SoundMgr from "../../core_wq/sound/SoundMgr";
import SoundType from "../../core_wq/sound/SoundType";
import GameStorage from "../../core_wq/game/GameStorage";
import GlobalData from "../../core_wq/db/GlobalData";
import HeroVO from "../../core_wq/db/vo/HeroVO";
import ColorUtil from "../../core_wq/utils/ColorUtil";
import MonsterSkillVO from "../../core_wq/db/vo/MonsterSkillVO";
import RandomSkill from "../../core_wq/game/skill/monster/RandomSkill";




export default class MainUI extends ui.moduleView.main.MainSceneUI {


    _startGameTimeLine: Laya.TimeLine;
    _isInit: boolean = false;
    _isLoading: boolean = true;
    _logoTimeLine: Laya.TimeLine;

    constructor() {
        super();

    }

    onEnable() {
        this.init();
    }

    public show(cb = null) {
        Laya.stage.event(GameEvent.SHOW_MAIN);
        this.bgSprite.alpha = 0.8;

        // cb&&cb();
        Laya.Tween.to(this.buyBtnGroup, { y: (this.buyBtnGroup.y - 550) }, 500, Laya.Ease.sineOut);
        Laya.Tween.to(this.logoImg, { y: (this.logoImg.y + 550) }, 500, Laya.Ease.sineOut, Laya.Handler.create(this.logoImg, cb));

        SoundMgr.Ins.playBg(SoundType.HOME_BG);
        // if (this._logoTimeLine == null) {
        //     this._logoTimeLine = EffectUtil.playLoopChangeY(this.logoImg);
        // }
        // this._logoTimeLine.play(0, true);
    }

    public hide(cb = null) {

        this.bgSprite.alpha = 1;
        Laya.Tween.to(this.logoImg, { y: (this.logoImg.y - 550) }, 500, Laya.Ease.sineOut);
        Laya.Tween.to(this.buyBtnGroup, { y: (this.buyBtnGroup.y + 550) }, 500, Laya.Ease.sineOut);

        Laya.stage.event(GameEvent.START_GAME);
        cb && cb();
        if (this._logoTimeLine != null) {
            this._logoTimeLine.pause();
        }

    }


    private init() {
        if (this.logoImg && this._isInit === false) {
            this._isInit = true;
            this.buyBtnGroup.y += 500;
            this.logoImg.y -= 500;
            this.refreshBg();

            this.show(() => {
                Game.reset();
                this.addEvent();
                this.refreshAttackLv();
                this.refreshAttackSpeedLv();
                this.refreshHpLv();
            })
        }

    }

    public addEvent() {
        Laya.stage.on(GameEvent.REFRESH_ATTACK_LV, this, this.refreshAttackLv);
        Laya.stage.on(GameEvent.REFRESH_ATTACK_SPEED_LV, this, this.refreshAttackSpeedLv);
        this.startGameLabel.on(Laya.Event.CLICK, this, this.onClickStartGame);

        this.buy1.on(Laya.Event.CLICK, this, this.buyAttack);
        this.buy2.on(Laya.Event.CLICK, this, this.buySpeed);
        this.buy2.on(Laya.Event.CLICK, this, this.buyAttack);
    }
    public buyAttack() {
        let heroVO: HeroVO = GlobalData.getData(GlobalData.HeroVO, Game.hero.playerData.attackLv + 1);
        if (Game.hero.playerData.gold < heroVO.attackCostGold) {
            SoundMgr.Ins.playEffect(SoundType.REJECT);
            EffectUtil.playTextEffect(Game.flyLayer, "金币不足", { x: this.width / 2, y: this.height / 2 + 100 });
            return;
        }
        EffectUtil.playCoinCostEffect(this.buy1);
        Game.hero.playerData.gold -= heroVO.attackCostGold;
        Game.hero.playerData.attackLv++;
        GameStorage.saveStorage();

        Laya.stage.event(GameEvent.REFRESH_ATTACK_LV);
        Game.hero.updateState();

    }

    public buySpeed(){
        let heroVO: HeroVO = GlobalData.getData(GlobalData.HeroVO, Game.hero.playerData.attackSpeedLv + 1);
        if (Game.hero.playerData.gold < heroVO.attackSpeedCostGold) {
            SoundMgr.Ins.playEffect(SoundType.REJECT);
            EffectUtil.playTextEffect(Game.flyLayer, "金币不足", { x: this.width / 2, y: this.height / 2 + 100 });
            return;
        }
        EffectUtil.playCoinCostEffect(this.buy2);
        Game.hero.playerData.gold -= heroVO.attackSpeedCostGold;
        Game.hero.playerData.attackSpeedLv++;
        GameStorage.saveStorage();

        Laya.stage.event(GameEvent.REFRESH_ATTACK_SPEED_LV);
        Game.hero.updateState();
    }
    public buyHp(){
        let heroVO: HeroVO = GlobalData.getData(GlobalData.HeroVO, Game.hero.playerData.hpLv + 1);
        if (Game.hero.playerData.gold < heroVO.hpCostGold) {
            SoundMgr.Ins.playEffect(SoundType.REJECT);
            EffectUtil.playTextEffect(Game.flyLayer, "金币不足", { x: this.width / 2, y: this.height / 2 + 100 });
            return;
        }
        EffectUtil.playCoinCostEffect(this.buy3);
        Game.hero.playerData.gold -= heroVO.hpCostGold;
        Game.hero.playerData.hpLv++;
        GameStorage.saveStorage();

        Laya.stage.event(GameEvent.REFRESH_HP_LV);
        Game.hero.updateState();
    }
    public refreshAttackLv() {
        let heroVO: HeroVO = GlobalData.getData(GlobalData.HeroVO, Game.hero.playerData.attackLv + 1);
        EffectUtil.refreshNumEffect(Number(this.attack.value), heroVO.attack, this.attack, 600);
        this.attackPrice.text = "x" + heroVO.attackCostGold;
        this.buy1.scale(0.9, 0.9);
        this.buy1.scale(1, 1);
    }
    public refreshAttackSpeedLv() {
        let heroVO: HeroVO = GlobalData.getData(GlobalData.HeroVO, Game.hero.playerData.attackSpeedLv + 1);
        EffectUtil.refreshNumEffect(Number(this.attackSpeed.value), heroVO.attackSpeed, this.attackSpeed, 600);
        this.attackSpeedPrice.text = "x" + heroVO.attackSpeedCostGold;
        this.buy2.scale(0.9, 0.9);
        this.buy2.scale(1, 1);
    }
    public refreshHpLv() {
        let heroVO: HeroVO = GlobalData.getData(GlobalData.HeroVO, Game.hero.playerData.hpLv);
        EffectUtil.refreshNumEffect(Number(this.hp.value), heroVO.hp, this.hp, 600);
        this.hpPrice.text = "x" + heroVO.hpCostGold;
        this.buy3.scale(0.9, 0.9);
        this.buy3.scale(1, 1);
    }
    public refreshBg() {
        this.bgSprite.loadImage("res/map/map" + Game.hero.playerData.mapBgId + ".png");
        this.bgSprite.alpha = 0.8;
        this._isLoading = false;
    }

    public setLoading() {
        this._isLoading = true;
        this.startGameLabel.text = "正在加载...";
    }

    public setStart() {
        this._isLoading = false;
        this.startGameLabel.text = "点击开始游戏";
    }


    public loadRes() {
        this.startGameLabel.visible = true;
        if (this._startGameTimeLine == null) {
            this._startGameTimeLine = EffectUtil.playTwinkleEffect(this.startGameLabel, null, true);
        }
        this._startGameTimeLine.resume();

        this.setLoading();

        let resArr = [];
        resArr.push("res/map/map" + Game.gameStage.stageVO.mapBgId + ".png");

        // console.log(Game.hero.bullet.fireDatas)
        Game.hero.bullet.fireDatas.map((fireData) => {
            fireData.bulletRes = "res/bullet/" + fireData.bullet.id + "/bullet" + fireData.bullet.id + ".lh"
            if (resArr.indexOf(fireData.bulletRes) <= -1) {
                resArr.push(fireData.bulletRes);
            }
        });
        // console.log(Game.hero.bullet.fireDatas)

        let monsterEffect = [];
        let isPushAllMonsterEffect;

        Game.gameStage.stageData.monsterDatas.map((monsterData: MonsterData) => {
            if (resArr.indexOf(monsterData.resPath) <= -1) {
                resArr.push(monsterData.resPath);
            }
            for (let i = 0; i < monsterData.skillIds.length; i++) {
                if (monsterData.skillIds[i] === 10010) {
                    isPushAllMonsterEffect = true;
                }
                let monsterSkillVO: MonsterSkillVO = GlobalData.getData(GlobalData.MonsterSkillVO, monsterData.skillIds[i]);
                if (monsterSkillVO.resPath && monsterSkillVO.resPath !== "0") {
                    // console.log(monsterSkillVO.resPath)
                    monsterEffect.push(monsterSkillVO.resPath);
                }
            }

        });
        if (isPushAllMonsterEffect === true) {
            let monsterSkillVOs: MonsterSkillVO[] = GlobalData.getAllValue(GlobalData.MonsterSkillVO);
            monsterSkillVOs.map((monsterSkillVO: MonsterSkillVO) => {
                if (monsterSkillVO.resPath && monsterSkillVO.resPath !== "0") {
                    resArr.push(monsterSkillVO.resPath);
                }
            })
        }
        else {
            resArr = resArr.concat(monsterEffect);
        }

        // console.log(resArr)
        Laya.loader.create(resArr, new Laya.Handler(this, this.onHandler));
    }

    onHandler() {
        this.setStart();
    }



    onClickStartGame() {
        if (this._isLoading) {
            return;
        }
        this.startGameLabel.visible = false;
        this._startGameTimeLine && this._startGameTimeLine.pause();
        this.hide();
        SoundMgr.Ins.playEffect(SoundType.ENTER_GAME);
    }


}